package com.hifar.test.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Instant;

/**
 * 统一响应格式
 * 用于标准化所有接口的返回格式
 */
public class UnifiedResponse<T> {
    
    @JsonProperty("code")
    private String code;
    
    @JsonProperty("message")
    private String message;
    
    @JsonProperty("data")
    private T data;
    
    @JsonProperty("timestamp")
    private long timestamp;
    
    @JsonProperty("success")
    private boolean success;
    
    public UnifiedResponse() {
        this.timestamp = Instant.now().toEpochMilli();
    }
    
    public UnifiedResponse(String code, String message, T data, boolean success) {
        this();
        this.code = code;
        this.message = message;
        this.data = data;
        this.success = success;
    }
    
    // 成功响应
    public static <T> UnifiedResponse<T> success(String message, T data) {
        return new UnifiedResponse<>("SUCCESS", message, data, true);
    }
    
    public static <T> UnifiedResponse<T> success(T data) {
        return new UnifiedResponse<>("SUCCESS", "操作成功", data, true);
    }
    
    // 失败响应
    public static <T> UnifiedResponse<T> failure(String code, String message, T data) {
        return new UnifiedResponse<>(code, message, data, false);
    }
    
    public static <T> UnifiedResponse<T> failure(String message, T data) {
        return new UnifiedResponse<>("ERROR", message, data, false);
    }
    
    // 认证错误响应
    @SuppressWarnings("unchecked")
    public static <T> UnifiedResponse<T> authError(String code, String message, String suggestion) {
        AuthErrorData errorData = new AuthErrorData(code, message, suggestion);
        return new UnifiedResponse<>(code, message, (T) errorData, false);
    }
    
    // 业务错误响应
    public static <T> UnifiedResponse<T> businessError(String code, String message, T data) {
        return new UnifiedResponse<>(code, message, data, false);
    }
    
    // Getters and Setters
    public String getCode() {
        return code;
    }
    
    public void setCode(String code) {
        this.code = code;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
    
    public boolean isSuccess() {
        return success;
    }
    
    public void setSuccess(boolean success) {
        this.success = success;
    }
    
    @Override
    public String toString() {
        return "UnifiedResponse{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", timestamp=" + timestamp +
                ", success=" + success +
                '}';
    }
    
    /**
     * 认证错误数据
     */
    public static class AuthErrorData {
        @JsonProperty("suggestion")
        private String suggestion;
        
        @JsonProperty("timestamp")
        private long timestamp;
        
        public AuthErrorData(String code, String message, String suggestion) {
            this.suggestion = suggestion;
            this.timestamp = Instant.now().toEpochMilli();
        }
        
        // Getters and Setters
        public String getSuggestion() { return suggestion; }
        public void setSuggestion(String suggestion) { this.suggestion = suggestion; }
        public long getTimestamp() { return timestamp; }
        public void setTimestamp(long timestamp) { this.timestamp = timestamp; }
    }
}
